/*
 * @Author: xuetao 3168581151@qq.com
 * @Date: 2025-01-06
 * @LastEditors: xuetao 3168581151@qq.com
 * @LastEditTime: 2025-03-15
 * @Description: 
 */
import 'package:chat_client/config/api_config.dart';
import 'package:chat_client/models/contact_model.dart';
import 'package:chat_client/providers/application_provider.dart';
import 'package:chat_client/providers/contact_provider.dart';
import 'package:chat_client/screens/mobile/widgets/appbar.dart';
import 'package:flutter/material.dart';
import 'package:chat_client/widgets/custom_global_widgets.dart';
import 'package:provider/provider.dart';

class ContactsScreen extends StatefulWidget {
  const ContactsScreen({super.key});

  @override
  State<StatefulWidget> createState() => _ContactsScreenState();
}

class _ContactsScreenState extends State<ContactsScreen> {
  final itemsLen = 3;

  @override
  Widget build(BuildContext context) {
    final ContactProvider model = Provider.of<ContactProvider>(context);
    final ApplicationProvider applyModel =
        Provider.of<ApplicationProvider>(context, listen: false);
    return Scaffold(
        appBar: CustomAppbar(title: '通讯录'),
        body: ListView.separated(
          physics: BouncingScrollPhysics(),
          itemCount: itemsLen + model.contactList.length, // 总共的项数，包含联系人和特殊项
          itemBuilder: (context, index) => buildList(model, applyModel, index),
          separatorBuilder: (context, index) {
            if (index == itemsLen - 1) {
              return const DividerWithText(text: '我的联系人');
            }
            return Divider(height: 1);
          },
        ));
  }

  buildList(ContactProvider model, ApplicationProvider applyModel, int index) {
    if (index < itemsLen) {
      return buildMenuItem(index, applyModel);
    } else {
      return buildContactList(
          model.contactList[index - itemsLen], model.navToContactPage);
    }
  }

  // 联系人项
  buildContactList(Contact contact, Function(void) navToContactPage) {
    return CListTile(
        imageUrl: AppConfig.baseUrl + contact.avatarUrl,
        title: contact.nickname,
        onTap: () => navToContactPage(contact));
  }

  // 功能项
  buildMenuItem(index, ApplicationProvider applyModel) {
    switch (index) {
      case 0:
        return CListTile(
            icon: Icons.person_add,
            title: '新朋友',
            iconColor: Colors.white,
            backgroundColor: Colors.amber,
            onTap: () => applyModel.navToNewFriends());
      case 1:
        return CListTile(
            icon: Icons.group,
            title: '群聊',
            iconColor: Colors.white,
            backgroundColor: Colors.green,
            onTap: () => applyModel.navToNewFriends());
      case 2:
        return CListTile(
            icon: Icons.label,
            title: '标签',
            iconColor: Colors.white,
            backgroundColor: Colors.blue,
            onTap: () => applyModel.navToNewFriends());
    }
  }
}
